//
//  BsTenantsView.m
//  SameWorld
//
//  Created by 李小朋 on 2017/3/29.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "BsTenantsView.h"
#import "CityChoose.h"
@interface BsTenantsView ()<UITextViewDelegate,UITextFieldDelegate>
/** 城市选择 */
@property (nonatomic ,strong) CityChoose *cityChoose;

@property (nonatomic ,strong) NSString *cityName;

@end

@implementation BsTenantsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"f1f1f1"];
        [self setup];
    }
    return self;
}

- (void)setup {
    
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, size_width, 291)];
    backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backView];
    
    // 姓名
    UITextField *userName = [[UITextField alloc] initWithFrame:(CGRectMake(12, 0, size_width, 44))];
    userName.placeholder = @"姓名";
    userName.font = [UIFont systemFontOfSize:12];
    userName.delegate =self;
    [backView addSubview:userName];
    self.userName = userName;
    
    UILabel *lineOne = [[UILabel alloc] initWithFrame:(CGRectMake(12, userName.frame.size.height, size_width, 1))];
    lineOne.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [backView addSubview:lineOne];
    
    // 手机号码
    UITextField *phone = [[UITextField alloc] initWithFrame:(CGRectMake(12, lineOne.frame.size.height+lineOne.frame.origin.y, size_width, 44))];
    phone.placeholder = @"手机号码";
    phone.delegate =self;
    phone.font = [UIFont systemFontOfSize:12];
    [backView addSubview:phone];
    self.userPhone = phone;
    
    UILabel *lineTwo = [[UILabel alloc] initWithFrame:(CGRectMake(12, phone.frame.size.height+phone.frame.origin.y, size_width, 1))];
    lineTwo.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [backView addSubview:lineTwo];
    
    // 验证码
    UITextField *validationCode = [[UITextField alloc] initWithFrame:(CGRectMake(12, lineTwo.frame.size.height+lineTwo.frame.origin.y, 100, 44))];
    validationCode.placeholder = @"验证码";
    validationCode.font = [UIFont systemFontOfSize:12];
    [backView addSubview:validationCode];
    self.validationCode = validationCode;
    
    // 发送验证码
    UIButton *sendCodeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    sendCodeBtn.frame = CGRectMake(self.frame.size.width - 102, lineTwo.frame.size.height+lineTwo.frame.origin.y + 9,90, 26);
    [sendCodeBtn setTitle:@"发送验证码" forState:(UIControlStateNormal)];
    [sendCodeBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    sendCodeBtn.backgroundColor = [UIColor colorWithHexString:@"#ff9900"];
    sendCodeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    sendCodeBtn.layer.masksToBounds = YES;
    sendCodeBtn.layer.cornerRadius = 2.0f;
    [sendCodeBtn addTarget:self action:@selector(sendCodeBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [backView addSubview:sendCodeBtn];
    self.sendCodeButton = sendCodeBtn;
    
    
    UILabel *lineThree = [[UILabel alloc] initWithFrame:(CGRectMake(12, validationCode.frame.size.height+validationCode.frame.origin.y, size_width, 1))];
    lineThree.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [backView addSubview:lineThree];
    
    // 门店名称
    UITextField *storesName = [[UITextField alloc] initWithFrame:(CGRectMake(12, lineThree.frame.size.height+lineThree.frame.origin.y, size_width, 44))];
    storesName.placeholder = @"门店名称";
    storesName.delegate= self;
    storesName.font = [UIFont systemFontOfSize:12];
    [backView addSubview:storesName];
    self.storesName = storesName;
    
    UILabel *lineFour = [[UILabel alloc] initWithFrame:(CGRectMake(12, storesName.frame.size.height+storesName.frame.origin.y, size_width, 1))];
    lineFour.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [backView addSubview:lineFour];
    
    
    // 城市地址
    UIButton *selecdCityBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    selecdCityBtn.frame = CGRectMake(12, lineFour.frame.size.height+lineFour.frame.origin.y, size_width, 44);
    self.cityName = @"选择您所在的城市和区域";
    [selecdCityBtn setTitle:[NSString stringWithFormat:@"%@",self.cityName] forState:(UIControlStateNormal)];
    [selecdCityBtn setTitleColor:[UIColor colorWithHexString:@"333333"] forState:(UIControlStateNormal)];
    selecdCityBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    selecdCityBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [selecdCityBtn addTarget:self action:@selector(CityBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [backView addSubview:selecdCityBtn];
    self.selecdCityBtn = selecdCityBtn;
    
    
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xuanze"]];
    image.frame = CGRectMake(selecdCityBtn.frame.size.width - 36, (selecdCityBtn.frame.size.height - 12)/2, 12, 12);
    [selecdCityBtn addSubview:image];
    
    
    UILabel *lineFive = [[UILabel alloc] initWithFrame:(CGRectMake(12, selecdCityBtn.frame.size.height+selecdCityBtn.frame.origin.y, size_width, 1))];
    lineFive.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [backView addSubview:lineFive];
    
    // 门店详细地址
    UITextView *storesAddress = [[UITextView alloc] initWithFrame:(CGRectMake(10, lineFive.frame.size.height + lineFive.frame.origin.y, size_width, 66))];
    storesAddress.text = @"门店详细地址";
    storesAddress.delegate =self;
    [backView addSubview:storesAddress];
    self.storesAddress = storesAddress;
    
    UILabel *sixFive = [[UILabel alloc] initWithFrame:(CGRectMake(0, storesAddress.frame.size.height+storesAddress.frame.origin.y, size_width, 1))];
    sixFive.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [backView addSubview:sixFive];
    
    // 确认提交
    UIButton *submitBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    submitBtn.frame = CGRectMake(12, backView.frame.origin.y + backView.frame.size.height + 28, size_width - 24, 44);
    submitBtn.backgroundColor = [UIColor colorWithHexString:@"ff9900"];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [submitBtn setTitle:@"确认提交" forState:(UIControlStateNormal)];
    submitBtn.layer.masksToBounds = YES;
    submitBtn.layer.cornerRadius = 2.0;
    [submitBtn addTarget:self action:@selector(submitBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:submitBtn];
    
}

#pragma textView Delegate
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    self.storesAddress.text = nil;
    if (size_height == 568) {
        self.y = self.y - 120;
    }

    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{

    if (size_height == 568) {
        self.y = self.y + 120;
    }
    [textView resignFirstResponder];
    [textView endEditing:YES];
    return YES;
}

#pragma textField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}



#pragma mark - 获取验证码
- (void)sendCodeBtnClick:(UIButton *)sender {
    
    self.sendCodeBtn();
}

#pragma mar - 选择城市按钮
- (void)CityBtn:(UIButton *)sender {
    [self endEditing:YES];
    YXWeakSelf
    self.cityChoose = [[CityChoose alloc] init];
    self.cityChoose.config = ^(NSString *province, NSString *city, NSString *town) {
//       sender.titleLabel.text = [NSString stringWithFormat:@"%@%@%@",province,city,town];
        weakSelf.cityName = [NSString stringWithFormat:@"%@-%@-%@",province,city,town];
        [sender setTitle:weakSelf.cityName forState:(UIControlStateNormal)];
    };
    [self addSubview:self.cityChoose];
    
}

#pragma mark - 提交按钮
- (void)submitBtnClicked:(UIButton *)sender {
    NSLog(@"确认提交");
    self.submitBtn();
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}


@end
